package w.z.x.framework.repository.wrappers;

import w.z.x.framework.repository.interfaces.SerialFunction;
import w.z.x.framework.repository.interfaces.Wrapper;
import w.z.x.framework.repository.structures.Segment;

import java.util.ArrayList;
import java.util.List;

/**
 * lambda更新封装
 *
 * @author 魏祖潇
 */
public class LambdaUpdateWrapper<T> extends AbstractWrapper<T, SerialFunction<T, ?>, LambdaUpdateWrapper<T>> {

    private final List<Segment> set = new ArrayList<>();

    /**
     * 获取更新片段
     *
     * @return 更新片段
     */
    public List<Segment> updateSegments() {
        return this.set;
    }

    /**
     * 设置
     *
     * @param field 字段
     * @param value 值
     * @return Wrapper
     */
    public LambdaUpdateWrapper<T> set(SerialFunction<T, ?> field, Object value) {
        Segment segment = Segment.build(SerialFunction.field(field), "=", value);
        this.set.add(segment);
        return this;
    }

    /**
     * 设置
     *
     * @param condition 是否符合条件
     * @param field     字段
     * @param value     值
     * @return Wrapper
     */
    public LambdaUpdateWrapper<T> set(boolean condition, SerialFunction<T, ?> field, Object value) {
        return condition ? set(field, value) : this;
    }

}
